<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 1:17 PM
 */
namespace Admin\Controller;
use Admin\Model;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ArrayObject;
use Zend\View\Model\ViewModel;
use Admin\Model\AdminConfigTable;
use Admin\Model\AdminConfig;
use Velacolib\Utility\Utility;
class ConfigController extends AbstractActionController
{
    protected $CategoriesTable;
    protected $service_locator_str;
    protected $configModel;

    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        $this->service_locator_str = 'Admin\Model\AdminConfigTable';
        $this->sm = $this->getServiceLocator();
        $this->configModel = $this->sm->get($this->service_locator_str);
       $user =  Utility::CheckLogin($this);
        if($user->user_type == 1){
            exit("Access denied!");
        }
        return parent::onDispatch($e);

    }

    public function init(){

    }



    public function indexAction()
    {
        $config = $this->configModel->fetch();
        $save = false;
        $request = $this->getRequest();
        if($request->isPost()){
             $post_Data = $this->params()->fromPost();
              foreach($post_Data as $key=>$val){
                    $this->configModel->save(array('value'=>$val),array('name'=>$key));
              }
            $save = true;
        }

        return new ViewModel( array(
          'config'=>$config ,
            'save'=>$save
        ));

    }


}